package com.dqhl.smartstable.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.dqhl.smartstable.R;
import com.dqhl.smartstable.base.BaseActivity;
import com.dqhl.smartstable.fragment.HomeFragment;
import com.dqhl.smartstable.fragment.LocationFragment;
import com.dqhl.smartstable.fragment.PersonalFragment;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private boolean quit = false; //设置退出标识
    private static final int HOME = 0;
    private static final int LOCATION = 1;
    private static final int PERSONAL = 2;
    private FragmentManager fragmentManager;
    private HomeFragment homeFragment;
    private LocationFragment locationFragment;
    private PersonalFragment personalFragment;

    private ImageView iv_home; //首页按钮
    private ImageView iv_location; //位置按钮
    private ImageView iv_personal; //我的按钮

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        iv_home = (ImageView) findViewById(R.id.iv_home);
        iv_location = (ImageView) findViewById(R.id.iv_location);
        iv_personal = (ImageView) findViewById(R.id.iv_personal);

        iv_home.setOnClickListener(this);
        iv_location.setOnClickListener(this);
        iv_personal.setOnClickListener(this);

        showFragment(HOME);

    }

    /**
     * 显示 fragment
     *
     * @param index
     */
    private void showFragment(int index) {
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        hideFragment(ft);
        initBottomBtn();
        switch (index){
            case HOME:
                if (homeFragment != null){
                    ft.show(homeFragment);
                } else {
                    homeFragment = new HomeFragment();
                    ft.add(R.id.flamelayout, homeFragment, "HomeFragment");
                }
                iv_home.setImageResource(R.drawable.ic_home_selected);
                break;
            case LOCATION:
                if (locationFragment != null){
                    ft.show(locationFragment);
                } else {
                    locationFragment = new LocationFragment();
                    ft.add(R.id.flamelayout, locationFragment, "LocationFragment");
                }
                iv_location.setImageResource(R.drawable.ic_location);
                break;
            case PERSONAL:
                if (personalFragment != null){
                    ft.show(personalFragment);
                } else {
                    personalFragment = new PersonalFragment();
                    ft.add(R.id.flamelayout, personalFragment, "PersonalFragment");
                }
                iv_personal.setImageResource(R.drawable.ic_personal_selected);
                break;
        }
        ft.commit();
    }

    private void initBottomBtn() {
        iv_home.setImageResource(R.drawable.ic_home);
        iv_location.setImageResource(R.drawable.ic_location);
        iv_personal.setImageResource(R.drawable.ic_personal);
    }

    /**
     * 隐藏 fragment
     *
     * @param ft
     */
    private void hideFragment(FragmentTransaction ft) {
        if (homeFragment != null){
            ft.hide(homeFragment);
        }
        if (locationFragment != null){
            ft.hide(locationFragment);
        }
        if (personalFragment != null){
            ft.hide(personalFragment);
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_home:
                showFragment(HOME);
                break;
            case R.id.iv_location:
                showFragment(LOCATION);
                break;
            case R.id.iv_personal:
                showFragment(PERSONAL);
                break;

        }
    }

    @Override
    public void onBackPressed() {
        if (!quit) {
            toast("再按一次退出程序");
            new Timer(true).schedule(new TimerTask() {
                @Override
                public void run() {
                    quit = false;
                }
            }, 2000);
            quit = true;
        } else {
            super.onBackPressed();
            finish();
        }
    }
}
